package com.zodiac.app.datathinking.szse.c0201;

import com.zodiac.app.datathinking.szse.Context;
import com.zodiac.app.datathinking.szse.Extractors;
import com.zodiac.app.datathinking.szse.Processor;
import lombok.Data;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Extractors(C0201Extractor.class)
@Component
public class C0201Processor implements Processor<List<Map<String, String>>, List<C0201Processor.Response>> {
    @Override
    public List<Response> process(List<Map<String, String>> request) {
        final C0201Extractor.Request extractRequest = (C0201Extractor.Request) Context.getCurrentContext().getExtractRequest();
        return request.stream().map(item -> {
            final Response response = new Response();
            response.setStockCode(item.get("zqdm"));
            response.setStockName(Jsoup.parse(item.get("gsjc")).text().trim());
            response.setCompanyName(item.get("gsqc"));
            response.setCompanySite(item.get("http"));
            response.setPlate(C0201Extractor.Request.Type.of(extractRequest.getTabkey()).getKey());
            final String sshymc = item.get("sshymc");
            if (Objects.nonNull(sshymc) && !sshymc.trim().isEmpty()) {
                final String[] codeWithName = sshymc.split("\\s+");
                response.setIndustryCode(codeWithName[0]);
                response.setIndustryName(codeWithName[1]);
            }
            return response;
        }).collect(Collectors.toList());
    }

    @Data
    public static class Response {
        // 股票代码
        private String stockCode;
        // 股票简称
        private String stockName;
        // 公司全称
        private String companyName;
        // 公司网址
        private String companySite;
        // 所属行业代码
        private String industryCode;
        // 所属行业名称
        private String industryName;
        // 所属板块
        private String plate;
    }
}
